# datetime : 15/8/2024 下午9:50
# -*-coding : utf-8 -*-
# Author : 油条测试
import requests, json


class RunByMethod():
    # 封装post请求
    def run_post(self, isJson, url, data=None, headers=None):
        if isJson:
            print(111111)
            res = requests.post(url=url, json=data, headers=headers)
        else:
            print(22222222)
            res = requests.post(url=url, data=json.loads(data), headers=headers)
        return res.json()

    # 封装get
    def run_get(self, isJson, url, data=None, headers=None):
        if isJson:
            print(33333)
            res = requests.get(url=url, json=data, headers=headers)
        else:
            print(44444)
            res = requests.get(url=url, data=data, headers=headers)
        # 序列化响应值，缩进2个空格,将中文正常展示
        # return json.dumps(res, indent=2, ensure_ascii=False)
        return res.json()

    # 运行总入口
    def run_main(self, methon, isJosn, url, data=None, headers=None):
        if methon == 'post':
            res = self.run_post(isJosn, url, data, headers)
        else:
            res = self.run_get(isJosn, url, data, headers)
        return json.dumps(res, indent=2, ensure_ascii=False)
